import java.util.Arrays;
import java.util.Random;

/**
 * 选择排序
 */
public class Demo02 {

  public static void main(String[] args) {
    int[] arr = {9, 8, 7, 6, 5, 4, 3, 2, 1};//定义数组

    //随机填充数组
    arr = new int[9];
    Random r = new Random();
    for(int i=0;i<arr.length;i++){
      arr[i]=r.nextInt(100);
    }

    arr = new int[]{1,2,3,4,5,6,7,8,9};

    System.out.println("排序前的原始数组:\t" + Arrays.toString(arr));

    for (int i = 1; i < arr.length; i++) {
      System.out.format("\n第%02d轮排序,比较%d次\n", i, arr.length - i);
      int minIndex = i - 1;
      for (int j = i; j < arr.length; j++) {
        if (arr[minIndex] > arr[j]) {
          minIndex = j;
        }
        System.out.format("第%02d轮第%d次:\t%s\n", i, j, Arrays.toString(arr));
      }
      if (minIndex > i - 1) {
        int temp = arr[minIndex];
        arr[minIndex] = arr[i-1];
        arr[i-1] = temp;
      }
      System.out.format("第%02d轮最小的索引%d:\t%s\n", i, minIndex, Arrays.toString(arr));

    }
  }

}
